#coding=utf-8
'''
Created on Aug 3, 2016

@author: wangpeng
'''

#coding=utf-8
'''
Created on Aug 2, 2016

@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common_setting.logging_save
import common_setting.desired_setting
import common_setting.config
import common_setting.Create_Log_Folder
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
icon_xpath = common_setting.config.huawei_BT_setting_icon_open()

print '1'

desired_setting = common_setting.desired_setting.desired_setting()
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_setting)
driver.start_activity("com.android.settings", ".HWSettings")
common_setting.logging_save.logging.info("app测试开始，开始检测蓝牙名字是否对应")
print time.ctime(),'打开蓝牙设置界面'
time.sleep(5)
driver.find_element_by_xpath("//android.widget.TextView[@text='蓝牙']").click()
time.sleep(5)
before = str("//android.widget.TextView[@text= '")
                                                                                                                                                                                                                                          
match = common_setting.config.bluetooth_id()
after = str("'] ")
name_detect = before+match+after
try:
     WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,name_detect))) #确认是否进入到主界面                       
     common_setting.logging_save.logging.info("经过确认，可以找到此广播ID值")
     print time.ctime(),"经过确认，可以找到此广播ID值"
     time.sleep(3)
     
except:
     pass

 #判断当前蓝牙是处于连接状态，还是断开状态
try:        
     WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//android.widget.TextView[@text='已连接']"))) #确认是否进入到主界面                       
     common_setting.logging_save.logging.info("经过确认，蓝牙已经处于连接状态")
     print time.ctime(),"经过确认，蓝牙已经处于连接状态"
 
except:
     time.sleep(3)
     driver.find_element_by_xpath(name_detect).click()
     time.sleep(4)
     common_setting.logging_save.logging.info("蓝牙重新建立连接")
     print time.ctime(),"蓝牙重新建立连接"
     time.sleep(10)
 
driver.quit()  #关掉server，重新进入酷狗界面
time.sleep(5)